import { ACOUNTAPIPREFIX, APPTOKENKEY, BASEAUTHTOKENFROMSBT, BASETOKENFROMSBT, SYSTEMCODE } from '@/common';
import { store } from '@/redux/store';
import { request } from '../request';
import type { YmResponse } from '../request/core';

type coopEntResponseData = {
  auditStatus: number;
  entId: string;
  entTypeCode: string;
  entTypeName: string;
  name: string;
  imgPath: string;
  entAccountVoList: {
    entAccountId: string;
    entAccountName: string;
    isDefault: 0 | 1;
  }[];
}[];

/**
 * @desc 获取当前用户的所有企业列表
//  */

export const coopEnt: (params: Record<string, number>) => Promise<YmResponse<coopEntResponseData>> = (params) =>
  request({
    method: 'post',
    url: ACOUNTAPIPREFIX + '/ent/user/api/account/coopEnt.htm',
    params,
    responseAsSuc: true,
    headers: {
      Clientsystem: 'Web',
      ClientType: 'WebEnt',
      baseEntId: store.getState().loginEnt.entId,
      baseuserid: localStorage.getItem(BASETOKENFROMSBT),
      AuthToken: localStorage.getItem(BASEAUTHTOKENFROMSBT),
    },
  });

/**
 * @deprecated
 * @desc sbt切换企业接口,废弃
 * @param {string} entId 企业id
 * @param {string} entAccountId 企业账户id
 */
export const affirmEnt: (
  entId: string,
  personId: string,
  entAccountId?: string,
) => Promise<YmResponse<coopEntResponseData>> = (entId, personId, entAccountId) =>
  request({
    method: 'post',
    url: ACOUNTAPIPREFIX + '/ent/user/api/account/affirmEnt.htm',
    params: {
      entId,
      entAccountId,
      personId,
      systemCode: SYSTEMCODE,
    },
    headers: {
      Clientsystem: 'Web',
      ClientType: 'WebEnt',
      baseEntId: store.getState().loginEnt.entId,
      baseuserid: localStorage.getItem(BASETOKENFROMSBT),
      AuthToken: localStorage.getItem(BASEAUTHTOKENFROMSBT),
    },
    responseAsSuc: true,
  });

/**
 * @description 安全教育项目 切换企业
 * @param params
 * @returns
 */
export const safeEduSwitchEnt = (params: any) =>
  // request({
  //   method: 'post',
  //   url: '/safe/switch',
  //   params,
  // });
  request({
    method: 'post',
    url: '/account/ent/user/api/account/affirmEnt.htm',
    params,
    headers: {
      ClientType: 'WebEnt',
      Equipment: 'Web',
      ClientSystem: 'Web',
      baseEntId: store.getState().loginEnt.entId,
      baseuserid: localStorage.getItem(BASETOKENFROMSBT),
      AuthToken: localStorage.getItem(BASEAUTHTOKENFROMSBT),
      platFormId: '4fc40d89c02a42489346e5d2eaac9444',
    },
  });

/**
 * @description 根据token获取entId
 * @param data
 * @returns
 */
export const getEntdIdBySafeEducToken = (data: any) =>
  request({
    method: 'post',
    url: '/safe/api/safePlanTask/getEntId',
    data: null,
    headers: {
      Authorization: localStorage.getItem(APPTOKENKEY) || data.token,
    },
  });
